<?php
// Copyright 2019 Hackware SpA <human@hackware.cl>
// This file is part of "Hackware Web Services Payment" and licensed under
// the terms of the GNU Affero General Public License version 3, or (at your
// option) a later version. You should have received a copy of this license
// along with the software. If not, see <https://www.gnu.org/licenses/>.

namespace Hawese\Payment\Http\Controllers;

use Hawese\Core\Http\Controllers\Controller;
use Illuminate\Support\Arr;
use Illuminate\Http\Request;
use Illuminate\Http\Response;

class GatewayController extends Controller
{
    public function index()
    {
        $gateways = config('gateways');

        $schemas = [];
        foreach (array_keys($gateways) as $gatewayKey) {
            $schemasKeys = array_keys($gateways[$gatewayKey]['schemas']);
            foreach ($schemasKeys as $schemaKey) {
                $schemas[$gatewayKey][$schemaKey] = route('gateways.schema', [
                    'gateway' => $gatewayKey,
                    'schema' => $schemaKey,
                ]);
            }
        }

        return ['schemas' => $schemas];
    }

    public function paymentMethods(Request $request, $schema)
    {
        $gateways = config('gateways');

        $paymentMethods = [];
        foreach (array_keys($gateways) as $gatewayKey) {
            $property = Arr::get(
                $gateways,
                "$gatewayKey.payment_methods.$schema"
            );
            $gatewayPaymentMethods = Arr::get(
                $gateways,
                "$gatewayKey.schemas.$schema.properties.$property.oneOf"
            );

            array_walk(
                $gatewayPaymentMethods,
                function (&$paymentMethod) use ($gatewayKey, $property) {
                    $paymentMethod['gateway'] = $gatewayKey;
                    $paymentMethod['property'] = $property;
                }
            );

            $paymentMethods = array_merge(
                $paymentMethods,
                $gatewayPaymentMethods
            );
        }

        return $paymentMethods;
    }

    public function schema(Request $request, $gateway, $schema)
    {
        return array_merge([
            '$schema' => 'http://json-schema.org/schema#',
            '$id' => route('gateways.schema', [
                'gateway' => $gateway,
                'schema' => $schema,
            ]),
            'type' => 'object',
        ], config("gateways.$gateway.schemas.$schema"));
    }

    /**
     * Receive notification from payment gateway, process and notify to other
     * custom endpoint.
     * @return mixed generally void.
     */
    public function notify(Request $request, $gateway)
    {
        $gatewayClass = config("gateways.$gateway.class");
        $gateway = new $gatewayClass();
        // This most probably will not return a response because
        // it's a method returned to IPN requests, returning status
        // OK is enough for most gateways
        return $gateway->acceptNotification($request->all());
    }

    /**
     * @return ['redirect_url' => $url] that's OK for current use cases,
     * but probably will change in the future.
     */
    public function purchase(Request $request, $gateway): array
    {
        $gatewayClass = config("gateways.$gateway.class");
        $gateway = new $gatewayClass();
        return $gateway->purchase($request->all());
    }
}
